package com.feinno.urcs.group.util;

import com.feinno.util.StringUtils;

import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author Li.Hongbo <lihongbo@feinno.com>
 */
public class MsrpContent {
	//From: <sip:+8613426240000@feinno.com>\r\nTo: <sip:125200019930442649@group.feinno.com>\r\nDateTime: 2014-12-14T20:05:43+08:00\r\n\r\nContent-Transfer-Encoding: base64\r\nContent-Type: text/plain;charset=UTF-8\r\nContent-Length: 8\r\n\r\nZ2hoaA==
	private String from;
	private String to;
	private String dateTime;
	private String contentTransferEncoding = "";
	private String contentType = "";
	private String charset;
	private String contentLength;
	private String ns = "";
	private String messageId = "";
	private String cc = "";
	private String silenceSupported = "";
	private String dispositionNotification = "";
	private String subject = "";

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getDispositionNotification() {
		return dispositionNotification;
	}

	public void setDispositionNotification(String dispositionNotification) {
		this.dispositionNotification = dispositionNotification;
	}

	public String getSilenceSupported() {
		return silenceSupported;
	}

	public void setSilenceSupported(String silenceSupported) {
		this.silenceSupported = silenceSupported;
	}

	public MsrpContent(byte[] bytes) {
		if (bytes != null) {
			String tmpContent = new String(bytes);
			String[] lines = tmpContent.split("\\r\\n");
			StringBuilder sb = new StringBuilder();
			AtomicBoolean append = new AtomicBoolean(false);
			Arrays.asList(lines).forEach(line -> {
				String l = line.trim();
				String tmpL = l.toLowerCase();
				if (l.length() == 0) return;
				if (tmpL.startsWith("From:".toLowerCase())) {
					setFrom(l.substring("From:".length(), l.length()));
					return;
				}
				if (tmpL.startsWith("To:".toLowerCase())) {
					setTo(l.substring("To:".length(), l.length()));
					return;
				}

				if (tmpL.startsWith("DateTime:".toLowerCase())) {
					setDateTime(l.substring("DateTime:".length(), l.length()));
					return;
				}

				if (tmpL.startsWith("Content-Transfer-Encoding:".toLowerCase())) {
					setContentTransferEncoding(l.substring("Content-Transfer-Encoding:".length(), l.length()));
					return;
				}

				if (tmpL.startsWith("charset:".toLowerCase())) {
					setCharset(l.substring("charset:".length(), l.length()));
					return;
				}
				if (tmpL.startsWith("Content-type:".toLowerCase())) {
					setContentType(l.substring("Content-type:".length(), l.length()));
					return;
				}
				if (tmpL.startsWith("Content-Length:".toLowerCase())) {
					setContentLength(l.substring("Content-Length:".length(), l.length()));
					return;
				}
				if (tmpL.startsWith("NS:".toLowerCase())) {
					setNS(l.substring("NS:".length(), l.length()));
					return;
				}
				if (tmpL.startsWith("imdn.Message-ID:".toLowerCase())) {
					setMessageID(l.substring("imdn.Message-ID:".length(), l.length()));
					return;
				}
				if (tmpL.startsWith("cc:".toLowerCase())) {
					setCc(l.substring("cc:".length(), l.length()));
					return;
				}
				if (tmpL.startsWith("CMCCfeature.silenceSupported:".toLowerCase())) {
					setSilenceSupported(l.substring("CMCCfeature.silenceSupported:".length(), l.length()));
					return;
				}
				if (tmpL.startsWith("imdn.Disposition-Notification:".toLowerCase())) {
					setDispositionNotification(l.substring("imdn.Disposition-Notification:".length(), l.length()));
					return;
				}
				if (tmpL.startsWith("Subject:".toLowerCase())) {
					setSubject(l.substring("Subject:".length(), l.length()));
					return;
				}
				sb.append(line).append("\r\n");

			});
			setContent(sb.substring(0, sb.lastIndexOf("\r\n")));
		}
	}

	public String getCc() {
		return cc;
	}

	public void setCc(String cc) {
		this.cc = cc;
	}

	private void setMessageID(String messageId) {
		this.messageId = messageId;
	}

	private void setNS(String ns) {
		this.ns = ns;
	}

	public String getNs() {
		return ns;
	}

	public String getMessageId() {
		return messageId;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getContentLength() {
		return contentLength;
	}

	public void setContentLength(String contentLength) {
		this.contentLength = contentLength;
	}

	public String getCharset() {
		return charset;
	}

	public void setCharset(String charset) {
		this.charset = charset;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		if (contentType.contains(";")) {
			String[] tmp = contentType.split(";");
			if (tmp.length > 1) {
				this.contentType = tmp[0].trim();
				if (tmp[1].contains("charset=")) {
					String tmpCharset = tmp[1].trim().substring("charset=".length(), tmp[1].length());
					if (tmpCharset != null) setCharset(tmpCharset);
				}
			}

		} else {
			this.contentType = contentType;
		}
	}

	public String getContentTransferEncoding() {
		return contentTransferEncoding;
	}

	public void setContentTransferEncoding(String contentTransferEncoding) {
		this.contentTransferEncoding = contentTransferEncoding;
	}

	public String getDateTime() {
		return dateTime;
	}

	public void setDateTime(String dateTime) {
		this.dateTime = dateTime;
	}

	public String getTo() {
		return to;
	}

	public void setTo(String to) {
		this.to = to;
	}

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	private String content;
}
